﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.produceDAL;
using dhSystemBLL.produceBLL;
using dhSystemBLL.basicData;
using baseSet.importExport;
using dhDataAcess.dataAcess.produceDataSetTableAdapters;
using dhDataAcess.dataAcess.stockDataSetTableAdapters ;
using dhDataAcess.dataAcess;
using baseSet;
using dhDataAcess.produceLinq;
using dhDataAcess.baseBLL;
using dhSystemBLL.orderData;
using dhsystem.Main;
using FastReport;
using dhsystem.produceForm.Stock;

namespace dhsystem.produceForm
{
    public partial class FrmorderStockOutMasterAdd : Form
    {
         //string boxnoI ;
         //   string MweightI ;
         //   string JweightI  ;
         //   string unitI  ;
         //   string qtyI  ;
         //   string pNameI ;
            dhDataAcess.dataAcess.produceDataSet.orderStockOutDetailDataTable ptable;
        //string packbillNo;
        produceDataSet.orderStockOutDetailDataTable orderStockOutDetailTable = new produceDataSet.orderStockOutDetailDataTable();
        orderStockOutDetailModel orderStockOutDetailRow = new orderStockOutDetailModel();
        orderStockOutBLL orderStockOut = new orderStockOutBLL();
        stockListBLL stockList = new stockListBLL();
        custListBLL cust = new custListBLL();
        deptBLL dept = new deptBLL();
        workerListBLL workerList = new workerListBLL();
        //orderStockOutMasterModel orderStockOutMasterRow = new orderStockOutMasterModel();
        produceDataSet.orderStockOutMasterDataTable orderStockOutTable = new produceDataSet.orderStockOutMasterDataTable();
        BindingSource b2;
        string state;
        public FrmorderStockOutMasterAdd(BindingSource b1, string _state)
        {
            InitializeComponent();
            state = _state;
            b2 = b1;
        }
        //打开窗口时绑定数据
        private void FrmorderStockOutMasterAdd_Load(object sender, EventArgs e)
        {
            //bindingSourcecustList.DataSource = cust.getcustListAll();
            //comboBox.DataSource = bindingSourcecustList;
            //comboBox.DisplayMember = "custName";
            //comboBox.ValueMember = "custNum";
            //bindingSourcestockList.DataSource = stockList.getstockListAll();
            //comboBoxstockName.DataSource = bindingSourcestockList;
            //comboBoxstockName.DisplayMember = "stockName";
            //comboBoxstockName.ValueMember = "stockName";
            bindingSourcedeptList.DataSource = dept.getdeptListAll();
            comboBoxdeptNum.DataSource = bindingSourcedeptList;
            comboBoxdeptNum.DisplayMember = "deptName";
            comboBoxdeptNum.ValueMember = "deptNum";
            bindingSourceworkerList.DataSource = workerList.getworkerListAll();
            comboBoxworkername.DataSource = bindingSourceworkerList;
            comboBoxworkername.DisplayMember = "name";
            comboBoxworkername.ValueMember = "workerNum";
            if (state == "新增")
            {
                textbillNo.Text = "S" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("订单成品出库单号");
                bindingSourceorderStockOutDetail.DataSource = orderStockOut.getsorderStockOutDetailAllBybillNo(textbillNo.Text);
                dGridDetail.DataSource = bindingSourceorderStockOutDetail;

            }
            if (state == "编辑")
            {
                comboBoxdeptNum.DataBindings.Add("SelectedValue", b2, "deptNum");
                textcustNum.DataBindings.Add("text", b2, "custNum");
                comboBoxworkername.DataBindings.Add("text", b2, "workername");
                textorderNo.DataBindings.Add("text", b2, "orderNo");
                textbillNo.DataBindings.Add("text", b2, "billNo");
                textaddress.DataBindings.Add("text", b2, "address");
                dateTimeoutDate.DataBindings.Add("value", b2, "outDate");
                textstockname.DataBindings.Add("text", b2, "stockName");
                textmark.DataBindings.Add("text", b2, "mark");
                bindingSourceorderStockOutDetail.DataSource = orderStockOut.getsorderStockOutDetailAllBybillNo(textbillNo.Text);
                dGridDetail.DataSource = bindingSourceorderStockOutDetail;
            }
            DcolumnSet.colSet(moduleInfo.orderStockOutDetail, dGridDetail);
            //dGridDetail.AllowUserToAddRows = false;
            dGridDetail.AllowUserToAddRows = true;
            this.KeyPreview = true;
            dGridDetail.EditMode = DataGridViewEditMode.EditOnEnter;
            comboBoxdeptNum.Text = "仓储部";
            comboBoxworkername.Text = "潘海燕";
        }
    
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textbillNo.Text == "")
            {
                MessageBox.Show("请输入库单号");
            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存成品出库库表信息！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                
                        produceDataSet.orderStockOutMasterRow orderStockOutMasterRow = orderStockOutTable.NeworderStockOutMasterRow();
                        //dhDataAcess.dataAcess.produceDataSet.orderStockOutDetailDataTable ptable = (dhDataAcess.dataAcess.produceDataSet.orderStockOutDetailDataTable)bindingSourceorderStockOutDetail.DataSource;
                        orderStockOutMasterRow.billNo = textbillNo.Text;
                        orderStockOutMasterRow.workername = comboBoxworkername.SelectedValue.ToString();
                        orderStockOutMasterRow.outDate = dateTimeoutDate.Value;
                        orderStockOutMasterRow.orderNo = textorderNo.Text;
                        orderStockOutMasterRow.billStatus = "开始出库";
                        orderStockOutMasterRow.custNum = basicSql.getcustNumbycustList(textcustNum.Text);
                        orderStockOutMasterRow.stockName = textstockname.Text;
                        orderStockOutMasterRow.deptNum = comboBoxdeptNum.SelectedValue.ToString();
                        orderStockOutMasterRow.address = textaddress.Text;
                        orderStockOutMasterRow.updateDate = DateTime.Now;
                        orderStockOutMasterRow.updateUser = GlobalInfo.UserName;
                        orderStockOutMasterRow.mark = textmark.Text;
                        orderStockOutTable.AddorderStockOutMasterRow(orderStockOutMasterRow);
                        orderStockOutTable.AcceptChanges();
                        orderStockOut.orderStockOutInsert(orderStockOutTable, ptable, state, textbillNo.Text, GlobalInfo.Date);
                        ptable.AcceptChanges();
                        bindingSourceorderStockOutMaster.DataSource = orderStockOut.getorderStockOutMasterAll();
                        //basicSql.getpackStockInMasterUpdatebillStatus(packbillNo);
                        foreach (produceDataSet.orderStockOutDetailRow Row in ptable)
                        {
                           

                            string str;
                            str = basicSql.getpackmark(Row.orderDetailNo);
                            str = str.Remove(str.Length - 1, 1);
                            basicSql.getorderStockOutDetailUpdate(Row.orderDetailNo,str,Row .billNo);//更新备注信息
                            if (Convert.ToDecimal(basicSql.getV_orderStockOutSUM(dGridDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString())) >= Convert.ToDecimal(basicSql.getqtybyorderDetail(dGridDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString())))
                            {
                                basicSql.getV_orderStockOutUpdate(dGridDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString(), "出库完成");//更新出库记录状态
                                basicSql.getVpackStockInSummarkUpdate(Row.orderDetailNo, "出库完成");//更新包装记录状态
                                basicSql.getorderDetailStatusUpdate(Row.orderDetailNo);//更新订单明细状态
                            }
                            basicSql.getVpackStockInSummarkUpdate(Row.orderDetailNo, "已出库");
                      
                        
                        }
                 
                        MessageBox.Show("保存成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //删除子件
        private void buttDel_Click(object sender, EventArgs e)
        {

            try
            {
                string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //编辑子件
        private void buttedit_Click(object sender, EventArgs e)
        {
            FrmorderStockOutDetailAdd orderStockOutDetail = new FrmorderStockOutDetailAdd(bindingSourceorderStockOutDetail, "编辑", textbillNo.Text);
            orderStockOutDetail.ShowDialog();
        }
        //添加子件
        private void buttAdd_Click(object sender, EventArgs e)
        {
            FrmorderStockOutDetailAdd orderStockOutDetail = new FrmorderStockOutDetailAdd(bindingSourceorderStockOutDetail, "新增", textbillNo.Text);
            orderStockOutDetail.ShowDialog();
        }
        //选择入库部门
        private void buttuserselect_Click(object sender, EventArgs e)
        {
            string field;
            field = "deptNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.deptList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxdeptNum.SelectedValue = productselect.pname;
            }
        }
        //选择出库人
        private void button4_Click(object sender, EventArgs e)
        {
            string field;
            field = "workerNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.workerList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxworkername.SelectedValue = productselect.pname;
            }
        }
       
        //选择订单号
        private void button1_Click(object sender, EventArgs e)
        {
            string field;
            field = "orderDetailNo";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.orderDetail, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textorderNo.Text  = productselect.pname;
            }
        }
        //订单号值发生改变之后
        private void textorderNo_TextChanged(object sender, EventArgs e)
        {
            try
            {
                textcustNum.Text = basicSql.getcustNumbyorderMaster(textorderNo.Text);
                textaddress.Text = basicSql.getaddressbyorderMaster(textorderNo.Text);
                //packStockInBLL packStockIn = new packStockInBLL();
                //produceDataSet.packStockInDetailDataTable packStockInDetailTable = packStockIn.getpackStockInDetailAllByorderDetailNo (textorderNo.Text);

                //foreach (produceDataSet.packStockInDetailRow packStockInRow in packStockInDetailTable)
                //{
                //    produceDataSet.orderStockOutDetailRow  orderStockOutRow = orderStockOutDetailTable.NeworderStockOutDetailRow ();
                //    cgshrkRow.acount = cgddRow.acount;
                //    cgshrkRow.billNo = textbillNo.Text;
                //    cgshrkRow.createDate = DateTime.Now;
                //    cgshrkRow.currency = cgddRow.currency;
                //    cgshrkRow.qty = cgddRow.qty;
                //    cgshrkRow.price = cgddRow.price;
                //    cgshrkRow.fqty = cgddRow.fqty;
                //    cgshrkRow.funit = cgddRow.funit;
                //    cgshrkRow.unit = cgddRow.unit;
                //    cgshrkRow.matNum = cgddRow.matNum;
                //    cgshrkRow.matName = cgddRow.matName;
                //    cgshrkRow.matSpec = cgddRow.matSpec;
                //    cgshrkRow.mark = cgddRow.mark;
                //    cgshrkDetailTable.AddcgshrkDetailRow(cgshrkRow);
                //}
                //bindingSourceorderStockOutDetail.DataSource = cgshrkDetailTable;
                //dGridDetail.DataSource = bindingSourceorderStockOutDetail;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        private void dGridDetail_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string field;
            //string fieldI;
            //fieldI = "billNo";
            string boxno = "boxno";
            string Mweight = "Mweight";
            string Jweight = "Jweight";
            string unit = "unit";
            string qty = "qty";
            string pName = "pName";
            field = "orderDetailNo";
            string billNo;
            billNo = textbillNo.Text;
            int colIndex = dGridDetail.Columns["orderDetailNo"].Index;
            int colIndexmark = dGridDetail.Columns["mark"].Index;
            if (colIndex == e.ColumnIndex)
            {
                FrmpackCount select = new FrmpackCount(moduleInfo.VpackCount, field, boxno, Mweight, Jweight, unit, qty, pName, billNo);
                DialogResult result = select.ShowDialog();
                if (result == DialogResult.Yes)
                {
                    //packbillNo = select.pnameI;
                  //  packbillNo = select.pname;
                    //boxnoI = select.boxnoI;
                    //MweightI = select.MweightI;
                    //JweightI = select.JweightI;
                    //unitI = select.unitI;
                    //qtyI = select.qtyI;
                    //pNameI = select.pNameI;
                   // dGridDetail.CurrentCell.Value = select.pname  ;
                   // bindingSourceorderStockOutDetail.DataSource = select.pTableI;
                   // dGridDetail.DataSource = bindingSourceorderStockOutDetail;
                    dGridDetail.DataSource = select.pTableI;
                    ptable = select.pTableI;
                }
            }
            if (colIndexmark == e.ColumnIndex)
            {
                Frmmark mark = new Frmmark();
                DialogResult result = mark.ShowDialog();
                if (result == DialogResult.Yes)
                {

                    dGridDetail.CurrentCell.Value = mark.mark;
                }
            }
        }

        private void dGridDetail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dGridDetail.Rows.Count > 0 && e.RowIndex >= 0)
            {
                DataGridViewRow row = dGridDetail.Rows[e.RowIndex];
                if (row.Cells[e.ColumnIndex].OwningColumn.Name == "orderDetailNo")
                {
                    //packStockInBLL packStockIn = new packStockInBLL();
                    //ViewDataSet.VpackStockInSumDataTable Table = packStockIn.getVpackStockInSumAllBybillNo (
                    //    packbillNo );
                    //foreach (ViewDataSet.VpackStockInSumRow Row in Table)
                    //{
                        //row.Cells["billNo"].Value = textbillNo.Text;
                        //row.Cells["pName"].Value =Row .pName ;
                        //row.Cells["unit"].Value = Row .unit ;
                        //row.Cells["boxno"].Value = Row.boxno;
                        //row.Cells["qty"].Value = Row.qty;
                        //if (!Row.IsJweightNull())
                        //{
                        //    row.Cells["weightJ"].Value = Row.Jweight;
                        //}
                        //else { row.Cells["weightJ"].Value = 0; }
                        // if (!Row.IsMweightNull ())
                        //{
                        //row.Cells["weightM"].Value = Row.Mweight;
                        //}
                        // else { row.Cells["weightM"].Value = 0; }
                   // row.Cells["orderDetailNo"].Value=
                    //row.Cells["billNo"].Value = textbillNo.Text;
                    //row.Cells["pName"].Value = pNameI ;
                    //row.Cells["unit"].Value =unitI ;
                    //row.Cells["boxno"].Value =boxnoI ;
                    //row.Cells["qty"].Value =qtyI ;
                    //if (JweightI != "")
                    //{
                    //    row.Cells["weightJ"].Value = JweightI;
                    //}
                    //else { row.Cells["weightJ"].Value = 0; }
                    // if (MweightI != "")
                    //{
                    //row.Cells["weightM"].Value = MweightI ;
                    //}
                    // else { row.Cells["weightM"].Value = 0; }
       
                    //}
                    bindingSourceorderStockOutDetail.AddNew();
                    textcustNum.Text = basicSql.getcustNumbyorderMaster (row.Cells["orderDetailNo"].Value.ToString());
                    textaddress.Text = basicSql.getaddressbyorderMaster (row.Cells["orderDetailNo"].Value.ToString());
                    //row.Cells["billNo"].Value = textbillNo.Text;
                    //row.Cells["pName"].Value = basicSql.getpnamebyorderDetail(row.Cells["orderDetailNo"].Value.ToString());
                    //row.Cells["unit"].Value = basicSql.getunitbyorderDetail(row.Cells["orderDetailNo"].Value.ToString());
                    //row.Cells["boxno"].Value = basicSql.getunitbyorderDetail(row.Cells["orderDetailNo"].Value.ToString());
                   


                }
            }
        }

        private void 打印送货单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string billNo = dGridDetail.CurrentRow.Cells["billNo"].Value.ToString();
            Report r = new Report();
            DataSet ds = new DataSet();
            ds.Tables.Add(orderStockOut.getV_orderStockOutAllBybillNo(billNo));
           // r.Load(@"E:\东华软件项目\报表\orderstockout.frx");
           r.Load(@"\\192.168.0.27\东华软件\报表\orderstockout.frx");
            r.RegisterData(ds);
           // r.Design();
           r.Show();
        }

        private void dGridDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void 打印香港送货单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string billNo = dGridDetail.CurrentRow.Cells["billNo"].Value.ToString();
            Report r = new Report();
            DataSet ds = new DataSet();
            ds.Tables.Add(orderStockOut.getV_orderStockOutAllBybillNo(billNo));
          //  r.Load(@"E:\东华软件项目\报表\orderstockoutxianggang.frx");
           r.Load(@"\\192.168.0.27\东华软件\报表\orderstockoutxianggang.frx");
            r.RegisterData(ds);
           // r.Design();
            r.Show();
        }

        private void 打印河源送货单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string billNo = dGridDetail.CurrentRow.Cells["billNo"].Value.ToString();
            Report r = new Report();
            DataSet ds = new DataSet();
            ds.Tables.Add(orderStockOut.getV_orderStockOutAllBybillNo(billNo));
          // r.Load(@"E:\东华软件项目\报表\orderstockoutheyuan.frx");
            r.Load(@"\\192.168.0.27\东华软件\报表\orderstockoutheyuan.frx");
            r.RegisterData(ds);
            //r.Design();
           r.Show();
        }

        private void toolSave_Click(object sender, EventArgs e)
        {
            if (textbillNo.Text == "")
            {
                MessageBox.Show("请输入库单号");
            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存成品出库库表信息！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {


                        produceDataSet.orderStockOutMasterRow orderStockOutMasterRow = orderStockOutTable.NeworderStockOutMasterRow();
                        //dhDataAcess.dataAcess.produceDataSet.orderStockOutDetailDataTable ptable = (dhDataAcess.dataAcess.produceDataSet.orderStockOutDetailDataTable)bindingSourceorderStockOutDetail.DataSource;
                        orderStockOutMasterRow.billNo = textbillNo.Text;
                        orderStockOutMasterRow.workername = comboBoxworkername.SelectedValue.ToString();
                        orderStockOutMasterRow.outDate = dateTimeoutDate.Value;
                        orderStockOutMasterRow.orderNo = textorderNo.Text;
                        orderStockOutMasterRow.billStatus = "开始出库";
                        orderStockOutMasterRow.custNum = basicSql.getcustNumbycustList(textcustNum.Text);
                        orderStockOutMasterRow.stockName = textstockname.Text;
                        orderStockOutMasterRow.deptNum = comboBoxdeptNum.SelectedValue.ToString();
                        orderStockOutMasterRow.address = textaddress.Text;
                        orderStockOutMasterRow.updateDate = DateTime.Now;
                        orderStockOutMasterRow.updateUser = GlobalInfo.UserName;
                        orderStockOutMasterRow.mark = textmark.Text;
                        orderStockOutTable.AddorderStockOutMasterRow(orderStockOutMasterRow);
                        orderStockOutTable.AcceptChanges();
                        orderStockOut.orderStockOutInsert(orderStockOutTable, ptable, state, textbillNo.Text, GlobalInfo.Date);
                        ptable.AcceptChanges();
                        bindingSourceorderStockOutMaster.DataSource = orderStockOut.getorderStockOutMasterAll();
                        //basicSql.getpackStockInMasterUpdatebillStatus(packbillNo);
                        foreach (produceDataSet.orderStockOutDetailRow Row in ptable)
                        {


                            string str;
                            str = basicSql.getpackmark(Row.orderDetailNo);
                            str = str.Remove(str.Length - 1, 1);
                            basicSql.getorderStockOutDetailUpdate(Row.orderDetailNo, str,Row .billNo );//更新备注信息
                            if (Convert.ToDecimal(basicSql.getV_orderStockOutSUM(dGridDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString())) >= Convert.ToDecimal(basicSql.getqtybyorderDetail(dGridDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString())))
                            {
                                basicSql.getV_orderStockOutUpdate(dGridDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString(), "出库完成");//更新出库记录状态
                                basicSql.getVpackStockInSummarkUpdate(Row.orderDetailNo, "出库完成");//更新包装记录状态
                                basicSql.getorderDetailStatusUpdate(Row.orderDetailNo);//更新订单明细状态
                            }
                            basicSql.getVpackStockInSummarkUpdate(Row.orderDetailNo, "已出库");


                        }

                        MessageBox.Show("保存成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }

        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolDel_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void toolAdd_Click(object sender, EventArgs e)
        {
            state = "新增";
            orderStockOutTable.Clear();
            textbillNo.Text = "S" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("订单成品出库单号");
            
            bindingSourceorderStockOutDetail.DataSource = orderStockOut.getsorderStockOutDetailAllBybillNo(textbillNo.Text);
            dGridDetail.DataSource = bindingSourceorderStockOutDetail;
            dGridDetail.AllowUserToAddRows = true;
            this.KeyPreview = true;
            dGridDetail.EditMode = DataGridViewEditMode.EditOnEnter;
            comboBoxdeptNum.Text = "仓储部";
            comboBoxworkername.Text = "潘海燕";
            dateTimeoutDate.Value = DateTime.Now;
        }
    }
}
